import pyHook
import time
import pythoncom
import threading
"""
    需求: 开条线程每1s循环输出一个"hello"，然后按Esc退出循环

"""

currentKeyValue = 0
keyBoardToAscii = {
    'Esc':27,
    '0':48
}

def onKeyboardEvent(event):
    global currentKeyValue 
    currentKeyValue = event.Ascii
    print(currentKeyValue)
    return True


def startKeyListen():     
   # 创建一个“钩子”管理对象     
   hm = pyHook.HookManager()      
   # 监听所有键盘事件     
   hm.KeyDown = onKeyboardEvent     
   # 设置键盘“钩子”     
   hm.HookKeyboard()  
   pythoncom.PumpMessages()


def dowork():
    global currentKeyValue
    print("dowork start!")
    while currentKeyValue != keyBoardToAscii['Esc']:
        time.sleep(1)
        print("hello")
    print("dowork end!")


def main():
    t1 = threading.Thread(target=dowork)
    t1.setDaemon(True)
    t1.start()
    startKeyListen()


if __name__ == "__main__":     
   main()